<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateAdminTable extends Migrator
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $table = $this->table('admin', ['comment' => '管理员', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);
        $table->addColumn('role_id', 'integer', ['limit'  =>  11, 'default' => 0, 'comment' => '角色组'])
            ->addColumn('account', 'string', ['limit'  =>  60, 'default' => '', 'comment' => '用户名'])
            ->addColumn('nickname', 'string', ['limit'  =>  60, 'default' => '', 'comment' => '昵称'])
            ->addColumn('mobile', 'string', ['limit'  =>  20, 'null' => true, 'default' => null, 'comment' => '手机号'])
            ->addColumn('password', 'string', ['limit'  =>  60, 'default' => '', 'comment' => '密码'])
            ->addColumn('salt', 'string', ['limit'  =>  30, 'default' => '', 'comment' => '密码盐'])
            ->addColumn('avatar', 'string', ['limit'  =>  255, 'default' => '', 'comment' => '头像'])
            ->addColumn('email', 'string', ['limit'  =>  60, 'null' => true, 'default' => null, 'comment' => '邮箱'])
            ->addColumn(Column::tinyInteger('login_fail')->setDefault(0)->setUnsigned()->setComment('登录失败次数'))
            ->addColumn('login_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '登录时间'])
            ->addColumn('login_ip', 'string', ['limit'  =>  20, 'null' => true, 'default' => null, 'comment' => '登录 ip'])
            ->addColumn(Column::enum('status', ['normal', 'disabled'])->setDefault('normal')->setComment('状态'))
            ->addColumn('create_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '更新时间'])
            ->addIndex('account', ['unique'  =>  true])
            ->addIndex('mobile', ['unique'  =>  true])
            ->addIndex('email', ['unique'  =>  true])
            ->create();
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        $table = $this->table('admin');
        $table->drop();
    }
}
